package com.example.smartgreenhouse.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LightStatusWebSocketServer {
    private final SimpMessagingTemplate messagingTemplate;

    @Autowired
    public LightStatusWebSocketServer(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void sendLightStatus(Long lightId, Integer isOn, Boolean isAuto, Long greenhouseId) {
        Map<String, Object> message = new HashMap<>();
        message.put("lightId", lightId);
        message.put("isOn", isOn);
        message.put("isAuto", isAuto);
        message.put("greenhouseId", greenhouseId);
        message.put("updateTime", System.currentTimeMillis());
        log.info("推送灯光状态WebSocket => 路径: /topic/light_status, 内容: {}", message);

        messagingTemplate.convertAndSend("/topic/light_status", message);
    }
}